import path from 'path'
import fs from 'fs'

const audioExtensions = ['.mp3', '.wav', '.ogg', '.aac', '.flac', '.m4a']

export const scanAudioFiles = async (dirPath) => {
  try {
    const files = await fs.promises.readdir(dirPath)

    const audioFiles = files
      .filter((file) => {
        const ext = path.extname(file).toLowerCase()
        return audioExtensions.includes(ext)
      })
      .map((file) => ({
        name: path.basename(file, path.extname(file)),
        path: path.join(dirPath, file),
        extension: path.extname(file).toLowerCase()
      }))

    return { success: true, files: audioFiles }
  } catch (error) {
    return { success: false, error: error.message }
  }
}
